﻿package
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import flash.net.*;
	import script.net.SocketRequestEvent;
	import script.net.SocketResponseEvent;
	import User;
	import Game;
	
	public class main extends MovieClip
	{
	
		private var isLocalMode = true; // TURN ME ON FOR OFFLINE PROGRAMMING
		private var myURL:String = "http://chodaidi.ikoaguild.com";
		protected var ourSocketClient:GameSocketClient;
		protected var ourUI:Object;
		// Game Variables
		protected static var mPlayerArr:Array = new Array();
		protected static var mOurGame:Game = new Game();
		protected static var mOurGameID:int;
		protected static var mOurUserID:int;
		protected static var mLoginUser:User;
		protected var ourTimer:Timer = new Timer(1000, 30);

		private var flashvars:Object;
		
		public function main()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			this.addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
		}

		protected function init(myEvent:Event):void
		{		
			ourSocketClient = new GameSocketClient();
			flashvars = this.loaderInfo.parameters;
			
			if(isLocalMode)
			{
				mLoginUser = new User("Empty Seat","http://chodaidi.ikoaguild.com/emptyseat.gif","0000");
			}
			else
			{
				mLoginUser = new User(flashvars.u_name, flashvars.u_pic, flashvars.fb_sig_user);
			}

			addChild(ourSocketClient);
			stage.addEventListener(SocketResponseEvent.CONNECTED, onConnected);
			stage.addEventListener(SocketResponseEvent.JOIN_GAME_CONFIRMATION, onJoinedGame);
			stage.addEventListener(SocketResponseEvent.PLAYER_JOINED, onPlayerJoined);
			stage.addEventListener(SocketResponseEvent.PLAYER_DISCONNECTED, onPlayerDisconnected);
			stage.addEventListener(SocketResponseEvent.PLAYER_STATE_CHANGED, onPlayerStateChanged);
			stage.addEventListener(SocketResponseEvent.OBJECT_CREATED, onObjectCreated);
			stage.addEventListener(SocketResponseEvent.OBJECT_STATE_CHANGED, onObjectStateChanged);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, processInput);
			
			ourUI = this["main_mc"];
			ourUI.init(); //Initialize GameIntro

			if(mLoginUser != null)
			{
				ourUI.output_txt.text = mLoginUser.userName;
			}
			else
			{
				
			}
			
			trace("==========Big 2 Yo version 0.0.2=============");
		}
		
		protected function cleanUp(myEvent:Event):void
		{
			stage.removeEventListener(SocketResponseEvent.JOIN_GAME_CONFIRMATION, onJoinedGame);
			stage.removeEventListener(SocketResponseEvent.PLAYER_JOINED, onPlayerJoined);
			stage.removeEventListener(SocketResponseEvent.PLAYER_DISCONNECTED, onPlayerDisconnected);
			stage.removeEventListener(SocketResponseEvent.PLAYER_STATE_CHANGED, onPlayerStateChanged);
			stage.removeEventListener(SocketResponseEvent.OBJECT_CREATED, onObjectCreated);
			stage.removeEventListener(SocketResponseEvent.OBJECT_STATE_CHANGED, onObjectStateChanged);
		}
		
		protected function processInput(event:KeyboardEvent):void
		{
			/*
			//SHIFT Keyboard Input
			if(event.keyCode == Keyboard.SHIFT)
			{
				stage.dispatchEvent(new SocketRequestEvent(SocketRequestEvent.CREATE_OBJECT, mOurGameID,{objectID: "Continuing", properties: mOurGame.getGameStateXML()}));
			}
			*/
		}
		
		protected function onConnected(myEvent:SocketResponseEvent):void
		{
			ourUI.changeStatus("Connected to Server.");
			
			mOurGameID = 1;
			mOurUserID = 1;

			stage.dispatchEvent(new SocketRequestEvent(SocketRequestEvent.JOIN_GAME, mOurGameID, {userID: mOurUserID}));
		}
		
		protected function onJoinedGame(myEvent:SocketResponseEvent):void
		{
			stage.dispatchEvent(new SocketRequestEvent(SocketRequestEvent.CHANGE_PLAYER_STATE, mOurGameID, {id: mLoginUser.userID, pic: mLoginUser.userPic, name: mLoginUser.userName}));
			
			mPlayerArr[int(myEvent.parameters.player_id)] = mLoginUser;
			mOurUserID = int(myEvent.parameters.player_id);
			
			trace ("USER JOINED");
			trace ("ID:"+int(myEvent.parameters.player_id)+" Name:"+mPlayerArr[int(myEvent.parameters.player_id)].userName);
			
			startGame();
		}
		
		protected function startGame():void
		{
			ourTimer.start();
			
			var request:URLRequest;
			if (isLocalMode)
			{
				request = new URLRequest("gameStage.swf");
			}
			else
			{
				request = new URLRequest(myURL+"/gameStage.swf");
			}
			var loader:Loader = new Loader()
			loader.load(request);
			addChild(loader);
		}

		protected function onPlayerJoined(myEvent:SocketResponseEvent):void
		{
			var newUser:User = new User(myEvent.parameters.properties.name, myEvent.parameters.properties.pic, myEvent.parameters.properties.id); 
			mPlayerArr[int(myEvent.parameters.player_id)] = newUser;

			trace ("PLAYER JOINED");
			trace ("ID:"+int(myEvent.parameters.player_id)+" Name:"+mPlayerArr[int(myEvent.parameters.player_id)].userName);
		}
		
		protected function onPlayerDisconnected(myEvent:SocketResponseEvent):void
		{
			trace ("PLAYER DISCONNECTED");
			trace ("ID:"+int(myEvent.parameters.player_id)+" Name:"+mPlayerArr[int(myEvent.parameters.player_id)].userName);
			
			mOurGame.setStatus(int(myEvent.parameters.player_id), "DISC");
			mPlayerArr[int(myEvent.parameters.player_id)] = null;
		}	
		
		protected function onPlayerStateChanged(myEvent:SocketResponseEvent):void
		{
			var newUser:User = new User(myEvent.parameters.properties.name, myEvent.parameters.properties.pic, myEvent.parameters.properties.id); 
			mPlayerArr[int(myEvent.parameters.player_id)] = newUser;

			trace ("PLAYER STATE CHANGE");
			trace ("ID:"+int(myEvent.parameters.player_id)+" Name:"+mPlayerArr[int(myEvent.parameters.player_id)].userName);
		}	
		
		protected function onObjectCreated(myEvent:SocketResponseEvent):void
		{
			trace ("OBJECT CREATED - BEFORE");
			trace ("TURN: "+mOurGame.turn);
			trace ("TYPE: "+mOurGame.type);
			trace ("1:"+mOurGame.hand1+" : "+mOurGame.status1);
			trace ("2:"+mOurGame.hand2+" : "+mOurGame.status2);
			trace ("3:"+mOurGame.hand3+" : "+mOurGame.status3);
			trace ("4:"+mOurGame.hand4+" : "+mOurGame.status4);
			trace ("TABLE: "+mOurGame.table);
			
			mOurGame.gameState(myEvent.parameters.properties.turn, myEvent.parameters.properties.type, 
							  myEvent.parameters.properties.hand1, myEvent.parameters.properties.hand2, 
							  myEvent.parameters.properties.hand3, myEvent.parameters.properties.hand4,
							  myEvent.parameters.properties.status1, myEvent.parameters.properties.status2,
							  myEvent.parameters.properties.status3, myEvent.parameters.properties.status4,
							  myEvent.parameters.properties.table);
			
			trace ("OBJECT CREATED - AFTER");
			trace ("TURN: "+mOurGame.turn);
			trace ("TYPE: "+mOurGame.type);
			trace ("1:"+mOurGame.hand1+" : "+mOurGame.status1);
			trace ("2:"+mOurGame.hand2+" : "+mOurGame.status2);
			trace ("3:"+mOurGame.hand3+" : "+mOurGame.status3);
			trace ("4:"+mOurGame.hand4+" : "+mOurGame.status4);
			trace ("TABLE: "+mOurGame.table);
		}
		
		protected function onObjectStateChanged(myEvent:SocketResponseEvent):void
		{
			trace ("OBJECT STATE CHANGE");
		}
		public static function get playerArr():Array { return mPlayerArr; }
		public static function get ourGame():Game { return mOurGame; }
		public static function get ourGameID():int { return mOurGameID; }
		public static function get ourUserID():int { return mOurUserID; }
		public static function get loginUser():User { return mLoginUser; }
	}
}